﻿using System;
using System.Windows;
using System.Windows.Threading;


namespace SL.MVVMS.RIA
{
    /// <summary>
    /// Class to Monitor which Thread is the UI, and Dispatch Actions there
    /// </summary>
    public static class UIDispatcher
    {
        private static Dispatcher UIThread;

        /// <summary>
        /// Call from the App Initialize
        /// </summary>
        public static void Initialize()
        {
            if (UIThread == null)
                UIThread = Deployment.Current.Dispatcher;
        }

        /// <summary>
        /// Call to send actions to the UI
        /// </summary>
        /// <param name="action"></param>
        public static void CallOnUI(Action action)
        {
            if (UIThread == null)
                return;

            if (UIThread.CheckAccess())
                action();
            else
                UIThread.BeginInvoke(action);
        }
    }
}
